package com.sz.common.pay.pingan.core.server;

import com.sz.common.pay.common.PayLogger;
import com.sz.common.pay.pingan.entity.common.CommonHeadResponse;
import com.sz.common.pay.pingan.exception.PayCommonException;
import com.sz.common.pay.pingan.util.PayExceptionUtil;

import java.lang.reflect.Field;
import java.util.HashMap;

/**
 * Function: 生成向银行请求报文的参数 存放银行发送报文的参数<br>
 * @author: siqishangshu <br>
 * @date: 2017-12-19 15:53:00
 */
public class ServerMapConverter {

    public static final String NULL_VALUE = "";
    private static PayLogger payLogger = PayLogger.getLogger(ServerMapConverter.class);
    /**
     * 生成向银行请求报文的参数
     *
     * @param commonHeadResponse
     * @param object
     * @param thirdLogNo
     * @return hashMap
     */
    public static HashMap toMap(CommonHeadResponse commonHeadResponse, Object object, String thirdLogNo) throws PayCommonException {
        HashMap<String,Object> hashMap = new HashMap();
        Class clazz = object.getClass();
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields){
            field.setAccessible(true);
            try {
                if (field.get(object) != null){
                    hashMap.put(field.getName(),field.get(object));
                }else{
                    hashMap.put(field.getName(), NULL_VALUE);
                }
            } catch (IllegalAccessException e) {
                payLogger.error(thirdLogNo,e.getMessage());
                PayExceptionUtil.throwPayCommonException(thirdLogNo,"object 转 map 异常"+e.getMessage());
            }
        }
        addHeadResponse(commonHeadResponse,thirdLogNo, hashMap);
        return hashMap;
    }

    private static void addHeadResponse(CommonHeadResponse commonHeadResponse,String thirdLogNo, HashMap<String, Object> hashMap) throws PayCommonException {
        Class clazz = commonHeadResponse.getClass();
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields){
            field.setAccessible(true);
            try {
                if (field.get(commonHeadResponse) != null){
                    hashMap.put(field.getName(),field.get(commonHeadResponse));
                }else{
                    hashMap.put(field.getName(), NULL_VALUE);
                }
            } catch (IllegalAccessException e) {
                payLogger.error(thirdLogNo,e.getMessage());
                PayExceptionUtil.throwPayCommonException(thirdLogNo,"object 转 map 异常"+e.getMessage());
            }
        }
    }

    /**
     * 存放银行发送报文的参数
     *
     * @param thirdLogNo
     * @param hashMap
     * @param clazz
     * @return
     */
    public static Object toObj(String thirdLogNo, HashMap hashMap, Class clazz) throws PayCommonException {
        Object instance = null;
        try {
            instance = clazz.newInstance();
            Field[] fields = clazz.getDeclaredFields();
            for (Field field : fields) {
                if (hashMap.containsKey(field.getName())){
                    field.setAccessible(true);
                    field.set(instance,hashMap.get(field.getName()));
                }
            }
        } catch (InstantiationException e) {
            payLogger.error(thirdLogNo,e.getMessage());
            PayExceptionUtil.throwPayCommonException(thirdLogNo," map 转 object 异常"+e.getMessage());
        } catch (IllegalAccessException e) {
            payLogger.error(thirdLogNo,e.getMessage());
            PayExceptionUtil.throwPayCommonException(thirdLogNo," map 转 object 异常"+e.getMessage());
        }
        return instance;
    }
}
